﻿<html> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15"/>
<title>Web剪刀手</title>
<link rel="stylesheet" type="text/css" href="options.css"/>
<link rel="shortcut icon" href="logo_32.png" type="image/x-icon" /> 
<style type='text/css'>

</style>
<script type="text/javascript" src="newwords.js"></script>
<script type="text/javascript" src="jquery-1.4.js"></script>
<script type="text/javascript" src="remember.js"></script>
<script type="text/javascript" src="http://webscissor.appspot.com/static/YX_Base.js"></script>
<script> 
var currentpage = 0;
var PAGE_SIZE=7;
var db=new DB_article();

var yxBase = new YXBase("http://webscissor.appspot.com",3);
var local_ngver = 0;

function Login()
{
    window.location.href = 
        "http://webscissor.appspot.com/Login?redi_url=/static/redirect.htm?redi_url=" 
        + chrome.extension.getURL("remember.htm");
}

function RefreshUpdateStatus()
{
	var spanStatus = getid("span_update_status");
	spanStatus.innerHTML = '下载：新 <font color=red>' + 
        spanStatus.need_update_count + 
        '</font> 篇/已更新 <font color=red>' + spanStatus.updated_count + 
        '</font> 篇,总 ' + spanStatus.list_length + ' 篇文章';
}

function RefreshSubmitStatus()
{
	var spanStatus = getid("span_submit_status");
    var htmSubmit = "";
	htmSubmit = '上传：新 <font color=red>' + spanStatus.need_submit_count 
	+ '</font> 篇/已上传 <font color=red>' + spanStatus.submited_count + '</font> 篇';
    if(spanStatus.submit_finished){
        htmSubmit += '<p>同步完成！</p>';
    }
	spanStatus.innerHTML = htmSubmit;
}
function UpdateData(gid,funOnFinish)
{
	yxBase.GetData(gid,function (res){
		var gid  = res[0];
		var gver = res[1];
		var del  = res[2];
		var item = JSON.parse(res[3]);
		var title = item[1];
		var body = item[2];
		var pageUrl = item[3];
		var addTime = new Date(item[4]);
        //alert(new Date((new Date()).getTime()));
        //alert(addTime);
		db.CheckUpdate(gid,gver,del,title,body,pageUrl,addTime);
		
		getid("span_update_status").updated_count += 1;
		RefreshUpdateStatus();
        
        if(getid("span_update_status").need_update_count 
            == getid("span_update_status").updated_count)
        {
            funOnFinish();
        }
	
	});
}
function CheckItemUpdate(itemDist,funOnFinish)
{
    var gid  = itemDist[0];
    var gver = itemDist[1];
    //var del  = itemDist[2];
    //if(!del)
    //{
    db.GetRowByGID(gid,function (rs){
        if(!rs) //本地没有此条目
        {
            getid("span_update_status").need_update_count += 1;
            RefreshUpdateStatus();
            UpdateData(gid,funOnFinish);
            return;
        }
        //alert("gver:" + rs.gver + "," + gver);
        if(rs.gver<gver)   //有此条目，但是版本不对
        {
            if(rs.mod) //本地也有修改？
            {
                alert("数据冲突，服务器数据和本地数据都有更改：" + rs.title);
                return;
            }
            getid("span_update_status").need_update_count += 1;
            RefreshUpdateStatus();
            UpdateData(gid,funOnFinish);
            return;
        }
    });
    //}
}
function DataUpdate(funOnFinish)
{
    yxBase.GetList(0,
        -1,
        function (listData)
        {
			getid("span_update_status").need_update_count = 0;
			getid("span_update_status").updated_count = 0;
			getid("span_update_status").list_length = listData.length;
			RefreshUpdateStatus();
            for(var i=0;i<listData.length;i++)
            {
                CheckItemUpdate(listData[i],funOnFinish);
            }
            if(getid("span_update_status").need_update_count==getid("span_update_status").need_update_count)
            {
                StartDataSubmit();
            }
        }
    );
};

function DataSubmit(funSuccess,funError)
{
    funSuccess = funSuccess||DefaultSuccess;
    funError = funError||DefaultError;
    RefreshSubmitStatus();
    
    db.GetRowToUpdate(function (rs){
        /*id: row['id'],
        gid: row['GID'],
        gver: row['GVER'],
        del: row['DEL'],
        mod: row['MOD'],
        title: row['title'],
        body: row['body'],
        pageUrl:row['pageUrl'],
        addTime:row['addTime']*/
        if(rs==null)
        {
            //funSuccess();
            //getid("span_username").innerHTML = "同步完成";
            getid("span_submit_status").submit_finished = true;
            RefreshSubmitStatus();
            return;
        }
        getid("span_submit_status").need_submit_count += 1;
        RefreshSubmitStatus();
        yxBase.AddData(
            JSON.stringify([rs.id,rs.title,rs.body,rs.pageUrl,rs.addTime]),
            function (res){
                var gid = res[0];
                if(gid<=0)
                {
                    return;
                }
                var gver = res[1];
                //getid("span_username").innerHTML = "同步：id" + rs.id + "," + rs.gid + "," + rs.gver;
                db.SetRowSyn(rs.id,gid,gver,rs.del);
                getid("span_submit_status").submited_count += 1;
                RefreshSubmitStatus();
                
                //递归调用，直到没有新数据为止
                DataSubmit(funSuccess,funError);
            }
        );
    });
};
function StartDataSubmit()
{
    //DataUpdate();
    getid("span_submit_status").submited_count = 0;
    getid("span_submit_status").submit_finished = false;
    getid("span_submit_status").need_submit_count = 0;
    yxBase.GetNGVER(
        function ()
        {
            DataSubmit();
        }
    );
    
};

function StartDataUpdate()
{
	yxBase.GetUserID(
		function (){
			DataUpdate(function (){
				StartDataSubmit();
			});
		},
		function(){
			var spanStatus = getid("span_update_status");
			spanStatus.innerHTML = "请先登录";
		}
	);
}

$(document).ready(function(){
    db.init();
	yxBase.GetUserID(
		function (){
			getid("span_username").innerHTML = '<a href="http://webscissor.appspot.com/u' + yxBase.user_id + '">网上主页</a> | ' + yxBase.user_mail
				+' | <a href="http://webscissor.appspot.com/Logout?redi_url=/static/redirect.htm?redi_url=' 
				+ chrome.extension.getURL("sync.htm") 
				+ '">退出</a>';
            yxBase.GetNGVER(
				function (){
					getid("span_update_status").innerHTML = '已登录，可以开始同步';
				}
            );
		},
		function(){
			getid("span_username").innerHTML = '<a href="http://webscissor.appspot.com/Login?redi_url=/static/redirect.htm?redi_url=' 
				+ chrome.extension.getURL("sync.htm") 
				+ '">登录</a>';
			getid("span_update_status").innerHTML = '请登录后开始同步';
		}
	);
 });
</script>
</head>
<body>
	<div id="debug"></div>
    <div id="main_container">
        <table width="90%" valign="top" align="center">
            <tr>
                <td align="left">
					<h1><img src="logo.png" width="30" />Web剪刀手</h1>
                </td>
                <td align="right">
                    <p>
                        <a href="./remember.htm">首页</a> | 
						<a href="./export.htm">导出</a> | 
						<a href="./import.htm">导入</a> | 
						<a href="http://code.google.com/p/webclip/" >Web剪刀手</a> |
                        <span id="span_username"><input type="button" value="登录" onclick="jscript:Login();"/></span> 
                        
                    </p>
                </td>
            </tr>
        </table>
<table cellpadding="5" class="settings-table">
	<tr>
		<td valign="top" width="75%">
			<table width="100%">
				<tr>
					<td>
					</td>
					<td align="right">
						<input type="button" value="开始同步" onclick="StartDataUpdate()" />
						<div><span id="span_update_status"></span></div>
						<div><span id="span_submit_status"></span></div>
					</td>
					<td align="right">
					</td>
				</tr>
			</table>
			<div id="div_words">
			</div>
		</td>
		<!-- <td valign="top" width="15%">
			<div id="div_tags">
			</div> -->
		</td>
	</tr>
</table>
</body>
</html>